﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KpBusiness.Layer;
using KpBusiness.Model;
using NHibernate;
using log4net;
using NHibernate.Criterion;

namespace KpBusiness.Service
{
	public class CandidateService
	{
		///Get Candidate
		// Update Candidate
		//Register Candidate
		private static readonly ILog log = LogManager.GetLogger("Services");


		/// <summary>
		/// Registers Candidate
		/// </summary>
		/// <param name="candidate">Register Candidate</param>
		/// <returns></returns>
		public void RegisterCandidate(Candidate candidate)
		{
			log.Debug("Started");
			ISession session = DataAccessLayerHelper.OpenWriterSession();
			
			ITransaction transaction = null;
			try
			{
				transaction = session.BeginTransaction();
				session.Save(candidate);
				session.Flush();
				transaction.Commit();
			}
			catch (Exception ex)
			{
				if (transaction != null && transaction.IsActive)
					transaction.Rollback();

				log.Error(ex);

			}
			finally
			{
				if (transaction != null)
					transaction.Dispose();

				if (session != null && session.IsConnected)
					session.Close();
			}

			log.Debug("End");
			
		}

		/// <summary>
		/// Update 
		/// </summary>
		/// <param name="candidate"></param>
		public void UpdateCandidate(Candidate candidate)
		{
			log.Debug("Started");
			ISession session = DataAccessLayerHelper.OpenWriterSession();

			ITransaction transaction = null;
			try
			{
				transaction = session.BeginTransaction();
				session.SaveOrUpdate(candidate);
				session.Flush();
				transaction.Commit();
			}
			catch (Exception ex)
			{
				if (transaction != null && transaction.IsActive)
					transaction.Rollback();

				log.Error(ex);

			}
			finally
			{
				if (transaction != null)
					transaction.Dispose();

				if (session != null && session.IsConnected)
					session.Close();
			}

			log.Debug("End");

		}

		/// <summary>
		/// Get all Candidates
		/// </summary>
		/// <param name="id">Provide Id</param>
		/// <returns>Return candidate</returns>
		public Candidate GetCanidate(int id)
		{
			log.Debug("Started");
			ISession session = DataAccessLayerHelper.OpenWriterSession();
			Candidate candidate = null;
			ITransaction transaction = null;
			try
			{
				transaction = session.BeginTransaction();
				candidate = session.CreateCriteria(typeof(Candidate), "Candidate")
					.Add(Restrictions.Eq("UserInformation.UserId", id)).UniqueResult<Candidate>();

				session.Flush();
				transaction.Commit();
			}
			catch (Exception ex)
			{
				if (transaction != null && transaction.IsActive)
					transaction.Rollback();

				log.Error(ex);

			}
			finally
			{
				if (transaction != null)
					transaction.Dispose();

				if (session != null && session.IsConnected)
					session.Close();
			}

			log.Debug("End");

			return candidate;
		}


		public CandidateSearch SearchCandidates(CandidateSearch searchData)
		{
			log.Debug("Started");
			if (searchData == null)
			{
				return null;
			}


			
			ISession session = DataAccessLayerHelper.OpenWriterSession();
			IList<Candidate> candidateList = null;
			ITransaction transaction = null;
			try
			{
				transaction = session.BeginTransaction();
				ICriteria criteria = session.CreateCriteria(typeof(Candidate), "Candidate");

				if (!string.IsNullOrWhiteSpace(searchData.SearchCandidate.Fname))
				{
					criteria.Add(Restrictions.Like("Fname", "%"+searchData.SearchCandidate.Fname+"%"));
				}


				if (!string.IsNullOrWhiteSpace(searchData.SearchCandidate.Mobile))
				{
					criteria.Add(Restrictions.Eq("Mobile", searchData.SearchCandidate.Mobile));
				}

				if (!string.IsNullOrWhiteSpace(searchData.SearchCandidate.CurrentLocation))
				{
					criteria.Add(Restrictions.Eq("CurrentLocation", searchData.SearchCandidate.CurrentLocation));
				}


				if (searchData.ExpFrom !=  0 && searchData.ExpTo != 0)
				{
					criteria.Add(Restrictions.Between("CurrentLocation", searchData.ExpFrom, searchData.ExpTo));
				}


				if (!string.IsNullOrWhiteSpace(searchData.SearchCandidate.Skills))
				{
					criteria.Add(Restrictions.Like("Skills", searchData.SearchCandidate.Skills));
				}


				if (searchData.SearchCandidate.CurrentCtc != 0 && searchData.SearchCandidate.ExpectedCtc != 0)
				{
					criteria.Add(Restrictions.Between("CurrentLocation", searchData.SearchCandidate.CurrentCtc, searchData.ExpTo));
				}
				criteria.SetMaxResults(1000);
				candidateList = criteria.List<Candidate>();
				searchData.SearchResult = candidateList;
				session.Flush();
				transaction.Commit();
			}
			catch (Exception ex)
			{
				if (transaction != null && transaction.IsActive)
					transaction.Rollback();

				log.Error(ex);

			}
			finally
			{
				if (transaction != null)
					transaction.Dispose();

				if (session != null && session.IsConnected)
					session.Close();
			}

			log.Debug("End");

			return searchData;

		}
	}
}
